package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by phazel on 12/04/14.
 */
public class Library
{
    ArrayList<String> books = new ArrayList<String>();
    ArrayList<String> checkedOutBooks = new ArrayList<String>();

    public Library() {
        books.add("The Agile Samurai - Jonathan Rasmusson");
        books.add("Debt: The first 5,000 years - David Graeber");
        books.add("Building Harlequin's Moon - Larry Niven and Brenda Cooper");
    }

    public void list()
    {
        System.out.println( "\n\tAvailable Books" );

        for(int i = 0; i < books.size(); i++)
        {
            System.out.println(i + ". " + books.get( i ));
        }
    }

    private void listCheckedOutBooks()
    {
        System.out.println( "\n\tBooks currently checked out:" );

        for(int i = 0; i < checkedOutBooks.size(); i++)
        {
            System.out.println(i + ". " + checkedOutBooks.get( i ));
        }
    }

    public void checkoutBook()
    {
        Scanner kb = new Scanner(System.in);

        while(true)
        {
            System.out.print("Select a book's number to check it out: ");

            String input = kb.nextLine();
            int selection = Integer.parseInt(input);

            try
            {
                if ( selection == -1 )
                {
                    System.out.println( "Quitting checkout" );
                    break;
                }
                String book = books.get( selection );
                checkedOutBooks.add( book );
                books.remove( selection );
                System.out.println("Thank you! Enjoy the book");
                break;
            }
            catch( Exception e )
            {
                System.out.println("That book is not available.");
            }
        }
    }

    public void returnBook()
    {
        listCheckedOutBooks();
        Scanner kb = new Scanner(System.in);

        while(true)
        {
            System.out.print( "\nSelect a book's number to return it: " );
            String input = kb.nextLine();
            int selection = Integer.parseInt(input);

            try
            {
                if ( selection == -1 )
                {
                    System.out.println( "Quitting book return" );
                    break;
                }
                String book = checkedOutBooks.get( selection );
                books.add( book );
                checkedOutBooks.remove( selection );
                System.out.println( "Thank you for returning the book." );
                break;
            }
            catch( Exception e )
            {
                System.out.println( "That is not a valid book to return." );
            }
        }
    }
}
